<?php
namespace AuctionCore\Account\Back;
use AuctionCore\System\AuctionException;
//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                         |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: songaimin@clcw.com.cn  Blog:http://blog.csdn.net/samxx8        |
// +------------------------------------------------------------------------+
//


/**
* 员工帐号组权限规则表
* @author       爱民
*/
class Rule
{

    public  $auth_object = null; //授权对象
    private $model = null; 

    public function __construct(){
         $b_auth = new \AuctionCore\Account\Back\Auth();
            
        $this->auth_object = $b_auth;
        $this->init_model();
    }
    
    /**
    * 启用规则
    * @return   void
    */
    public function enable($id)
    {
         $where = "id='".$id."'";        
         $ok = $this->update(array('status'=>'0'),$where);
    }
    
    /**
    * 禁用规则
    * @return   void
    */
    public function disable($id)
    {
          $where = "id='".$id."'";        
          $ok = $this->update(array('status'=>'1'),$where);
    }
    
    /**
    * 创建规则
    * 返回id
    * @param    Object $info    规则信息                              
    * @return   int
    */
    public function create($info)
    {
        $insertId = $this->model->add($info);
        return $insertId;
    }
    
    /**
    * 删除规则
    * @param    int $gid    规则id
    * @return   void
    */
    public function del($gid)
    {
         $ok = $this->model->delete($gid);
         return $ok;
    }
    
    /**
    * 更新规则
    * @param    Object $info    规则信息                              
    * @param    $where   条件信息 
    * @return   boolean
    */
    public function update($info,$where)
    {
         $ok = $this->model->where($where)->save($info);
         return $ok;

    } 

    public function get_list(){
        $m = $this->model;
        $list = array();
        $p_rules = $m->where('pid=0')->select();
        foreach ($p_rules as $key => $row) {
            $rules = $m->where('pid='.$row['id'])->select();
            array_push($list,$row);
            if($rules && count($rules)>0){                
                $list = array_merge($list,$rules);    
            }
        }
        //dump($list);exit;
        return $list;        
    }

   
    private function init_model(){
        if(is_null($this->model)){
             $auth_db_cfg = $this->auth_object->get_config();    
             $this->model = M($auth_db_cfg["AUTH_RULE"]);
        }
     }
}

?>